package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.come.Frame.DuelBoardJframe;

import com.tool.btn.DuelBoardBtn;
import com.tool.tcpimg.UIUtils;

public class DuelBoardJpanel extends JPanel {

    // 排行榜信息表格
    private JTable tablePankList;
    // 排行榜信息表格中的对象
    private DefaultTableModel tableModel;
    private Vector<Vector<String>> verVectors;// 设置所有排行榜信息的集合
    private Vector<String> verStrings;// 放置排行榜信息的集合
    private DuelBoardBtn showBtn; // 展示面板
    private DuelBoardJframe duelBoardJframe;
    private boolean showType = true; // 展示与隐藏

    public DuelBoardJpanel(DuelBoardJframe duelBoardJframe) {
        this.setPreferredSize(new Dimension(210, 188));
        this.setLayout(null);
        this.setOpaque(false);

        this.duelBoardJframe = duelBoardJframe;

        tableModel = new DefaultTableModel();
        verVectors = new Vector<Vector<String>>();
        verStrings = new Vector<String>();
        verVectors.add(verStrings);
        Vector<String> vector3 = new Vector<String>();
        vector3.add("");
        vector3.add("");
        vector3.add("");
        tableModel.setDataVector(verVectors, vector3);

        tablePankList = new JTable() {

            @Override
            public boolean isCellEditable(int row, int column) {
                // TODO Auto-generated method stub
                return false;
            }
        };

        // 帮派成员信息表格
        tablePankList.setShowGrid(false);

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                // if (row < 3)
                // setForeground(Color.yellow);
                // else
                setForeground(Color.white);

                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
        };
        renderer.setHorizontalAlignment(SwingConstants.CENTER);
        tablePankList.setDefaultRenderer(Object.class, renderer);

        JTableHeader header = tablePankList.getTableHeader();

        // 设置表头的高度
        header.setPreferredSize(new Dimension(header.getWidth(), 0));
        tablePankList.getTableHeader().setBackground(UIUtils.Color_BACK);
        tablePankList.getTableHeader().setVisible(true);
        tablePankList.getTableHeader().setBorder(BorderFactory.createLineBorder(Color.ORANGE));

        tablePankList.setSelectionBackground(UIUtils.Color_BACK);
        tablePankList.setSelectionForeground(Color.white);
        tablePankList.setForeground(Color.white);
        tablePankList.setFont(UIUtils.TEXT_FONT);
        tablePankList.setBackground(UIUtils.Color_BACK);
        tablePankList.setModel(tableModel);

        // 设置列间距
        tablePankList.getColumnModel().getColumn(0).setPreferredWidth(90);
        tablePankList.getColumnModel().getColumn(1).setPreferredWidth(60);
        tablePankList.getColumnModel().getColumn(2).setPreferredWidth(60);
        // 设置行宽
        tablePankList.setRowHeight(25);
        tablePankList.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tablePankList.isCellEditable(1, 1);
        tablePankList.setEnabled(true);
        tablePankList.setBounds(0, 53, 210, 188 - 53);
        this.add(tablePankList);

        getShowBtn();

    }

    // 修改排行榜数据
    public void getBoardData(String boardData) {
        tableModel.getDataVector().clear();
        if (boardData == null || "".equals(boardData)) {
            return;
        }
        String[] dataList = boardData.split("\\|");
        for (int i = 0; i < dataList.length; i++) {
            String[] data = dataList[i].split("&");
            Vector<String> vector = new Vector<>();
            for (int j = 0; j < data.length; j++) {
                vector.add(data[j]);
            }
            tableModel.addRow(vector);
        }
    }

    // 背景
    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 技能
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S146.png");
        }

        g.drawImage(icon.getImage(), 0, 0, 210, 188, this);

    }

    public JTable getTablePankList() {
        return tablePankList;
    }

    public void setTablePankList(JTable tablePankList) {
        this.tablePankList = tablePankList;
    }

    public DefaultTableModel getTableModel() {
        return tableModel;
    }

    public void setTableModel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public Vector<Vector<String>> getVerVectors() {
        return verVectors;
    }

    public void setVerVectors(Vector<Vector<String>> verVectors) {
        this.verVectors = verVectors;
    }

    public Vector<String> getVerStrings() {
        return verStrings;
    }

    public void setVerStrings(Vector<String> verStrings) {
        this.verStrings = verStrings;
    }

    public DuelBoardBtn getShowBtn() {
        if (showBtn == null) {
            showBtn = new DuelBoardBtn("skin/8100226/button/9.png", 1, "", 1, this);
            showBtn.setBounds(210 - 18, 0, 18, 18);
            this.add(showBtn);
        }
        return showBtn;
    }

    public void setShowBtn(DuelBoardBtn showBtn) {
        this.showBtn = showBtn;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public DuelBoardJframe getDuelBoardJframe() {
        return duelBoardJframe;
    }

    public void setDuelBoardJframe(DuelBoardJframe duelBoardJframe) {
        this.duelBoardJframe = duelBoardJframe;
    }

    public boolean isShowType() {
        return showType;
    }

    public void setShowType(boolean showType) {
        this.showType = showType;
    }

}
